
package com.wulian.common.suport;


import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;

/**
 * The Ribbon filter context holder.
 *
 * @author Jakub Narloch
 */
public class RibbonFilterContextHolder {


    public static final HystrixRequestVariableDefault<RibbonFilterContext> contextHolder = new HystrixRequestVariableDefault<RibbonFilterContext>(){
        @Override
        public RibbonFilterContext initialValue() {

            return new DefaultRibbonFilterContext();
        }
        @Override
        public void shutdown(RibbonFilterContext value) {
            // do nothing by default
            ((DefaultRibbonFilterContext)value).clear();
        }
    };



    /**
     * Retrieves the current thread bound instance of {@link RibbonFilterContext}.
     *
     * @return the current context
     */
    public static RibbonFilterContext getCurrentContext() {
        if (!HystrixRequestContext.isCurrentThreadInitialized()) {
            HystrixRequestContext.initializeContext();
        }
        return contextHolder.get();
    }

    /**
     * Clears the current context.
     */
    public static void clearCurrentContext() {
        contextHolder.remove();
    }
}
